using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PACKETPARSERMAX20.CORE;

namespace PACKETPARSERMAX20.CORE.DEFINITIONS
{
    public class SMSG_AURA_UPDATE_DEF : DefinitionBase
    {
        public SortedList<int, AuraUpdate> auraupdatelist;

        public IList<KeyValuePair<int, AuraUpdate>> validauraupdatelist
        {
            get
            {
                return auraupdatelist.Where(t => t.Value.notcasterguid == 0).ToList();
            }
        }

        public override bool Process()
        {
            ResetPosition();

            if (AvailableBytes  <= 5){ Warning = "bad packet."; return true;}
            auraupdatelist = new SortedList<int, AuraUpdate>();

            ulong guid = popGuid();

           // InitLog(); //start the log
            //if (LogIt) LogAppendLine("UInt64", "guid", guid, true);
            int i = 0;

            while (AvailableBytes > 0)
            {
                AuraUpdate auraupdate = new AuraUpdate();

                auraupdate.index = i;
                auraupdate.auraslot = ReadByte();
                auraupdate.id = ReadUInt32();

                if (auraupdate.id != 0)
                {
                    auraupdate.auraflags = (AURA_FLAGS)ReadByte();
                    auraupdate.auralevel = ReadByte();
                    auraupdate.amount = ReadByte();


                    if ((auraupdate.auraflags & AURA_FLAGS.AFLAG_NOT_CASTER) == 0)
                    {
                        auraupdate.notcasterguid = popGuid();                                   // guid
                    }

                    if ((auraupdate.auraflags & AURA_FLAGS.AFLAG_DURATION) != 0)
                    {
                        auraupdate.auramaxduration = ReadUInt32();
                        auraupdate.auraduration = ReadUInt32();
                    }
                }

                //auraupdate.LogIt(this);

                auraupdatelist.Add(i, auraupdate);

                i++;
            }

            var obj = Core.GetObject(guid);
            if (obj != null && obj.TypeID == TypeID.TYPEID_UNIT && auraupdatelist.Count > 0)
            {
                obj.UpdateAuraInfo(this);
            }

            return Validate();
        }

        public class AuraUpdate
        {
            public int index { get; set; }
            public byte auraslot { get; set; }
            public uint id { get; set; }
            public AURA_FLAGS auraflags { get; set; }
            public byte auralevel { get; set; }
            public byte amount { get; set; }
            public ulong notcasterguid { get; set; }
            public ulong auramaxduration { get; set; }
            public ulong auraduration { get; set; }

            //public void LogIt(DefinitionBase defbase)
            //{
            //    if (defbase.LogIt)
            //    {
            //     //   defbase.LogAppendLine("byte", "AuraSlot", auraslot, index);
            //     //   defbase.LogAppendLine("UInt32", "id", id, index);

            //        if (id != 0)
            //        {
            //         //   defbase.LogAppendLine("byte", "auraFlags", (uint)auraflags, index, "", auraflags.DumpFlags());
            //         //   defbase.LogAppendLine("byte", "AuraLevel", auralevel, index);
            //         //   defbase.LogAppendLine("byte", "Amount", amount, index);

            //            if ((auraflags & AURA_FLAGS.AFLAG_NOT_CASTER) == 0)
            //            {
            //             //   defbase.LogAppendLine("UInt64", "notcasterguid", notcasterguid, index, "AFLAG_NOT_CASTER", true);
            //            }

            //            if ((auraflags & AURA_FLAGS.AFLAG_DURATION) != 0)
            //            {
            //             //   defbase.LogAppendLine("UInt32", "AuraMaxDuration", auramaxduration, index, "AFLAG_DURATION");
            //             //   defbase.LogAppendLine("UInt32", "AuraDuration", auraduration, index, "AFLAG_DURATION");
            //            }

            //        }
            //    }
            //}

            public override string ToString()
            {

                return string.Format("index: {0},auraslot: {1},id: {2},auraflags: {3},auralevel: {4},amount: {5},notcasterguid: {6},auramaxduration: {7},auraduration: {8}",
                                index, auraslot, id, (uint)auraflags, auralevel, amount, notcasterguid, auramaxduration, auraduration);
            }



        }
    }
}